/**
 * Created by yhg97p on 2016/9/26.
 */
define(['angular','router', 'cookies', 'oclazyload', 'bootstrap','sanitize','scrollbars','toImages'],function(){
    var app = angular.module("qiyepeixun", ['ui.router', 'ngCookies', 'ui.bootstrap', 'oc.lazyLoad','ngSanitize','jQueryScrollbar']);
    //定义全局变量
    app._baseUrl = (window.location.href.split('/app'))[0] + '/index.php?s=';
    // app._baseUrl = 'http://10.64.83.134/qiyepeixun/index.php?s=';
    // app._baseUrl = 'http://139.129.236.203//qiyepeixun/index.php?s=';
    var v = "1-1-1";
    app.resolveScriptDeps = function(dependencies) {
        return function($q, $rootScope) {
            var deferred = $q.defer();
            var dependencies2 = dependencies;
            if (Array.isArray(dependencies2)) {
                for (var i = 0; i < dependencies2.length; i++) {
                    dependencies2[i] += "?v=" + v;
                }
            } else {
                dependencies2 += "?v=" + v;//v是版本号
            }
            $script(dependencies2, function() {
                // all dependencies have now been loaded by $script.js so resolve the promise
                $rootScope.$apply(function() {
                    deferred.resolve();
                });
            });

            return deferred.promise;
        }
    };
    //配置路由
    app.config(function($controllerProvider,$compileProvider,$filterProvider,$provide){
            app.register = {
                //得到$controllerProvider的引用
                controller : $controllerProvider.register,
                //同样的，这里也可以保存directive／filter／service的引用
                directive: $compileProvider.directive,
                filter: $filterProvider.register,
                service: $provide.service
            };
        })
        .config(['$httpProvider', function ($httpProvider) {
            //$httpProvider.defaults.withCredentials = true;
            // http://stackoverflow.com/questions/17289195/angularjs-post-data-to-external-rest-api
            $httpProvider.defaults.useXDomain = true;
            delete $httpProvider.defaults.headers.common['X-Requested-With'];
            // Use x-www-form-urlencoded Content-Type
            $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
            $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
            // Override $http service's default transformRequest
            $httpProvider.defaults.transformRequest = [function(data)
            {
                /**
                 * The workhorse; converts an object to x-www-form-urlencoded serialization.
                 * @param {Object} obj
                 * @return {String}
                 */
                var param = function(obj)
                {
                    var query = '';
                    var name, value, fullSubName, subName, subValue, innerObj, i;
                    for(name in obj)
                    {
                        value = obj[name];
                        if(value instanceof Array)
                        {
                            for(i=0; i<value.length; ++i)
                            {
                                subValue = value[i];
                                fullSubName = name + '[' + i + ']';
                                innerObj = {};
                                innerObj[fullSubName] = subValue;
                                query += param(innerObj) + '&';
                            }
                        }
                        else if(value instanceof Object)
                        {
                            for(subName in value)
                            {
                                subValue = value[subName];
                                if(subValue != null){
                                    // fullSubName = name + '[' + subName + ']';
                                    fullSubName = name + '.' + subName;
                                    // fullSubName =  subName;
                                    innerObj = {};
                                    innerObj[fullSubName] = subValue;
                                    query += param(innerObj) + '&';
                                }
                            }
                        }
                        else if(value !== undefined ) //&& value !== null
                        {
                            query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
                        }
                    }
                    return query.length ? query.substr(0, query.length - 1) : query;
                };
                return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
            }];
            $httpProvider.defaults.withCredentials = true;    //允许cookie传递
            $httpProvider.interceptors.push('securityInterceptor'); //注册全局权限控制
        }])
        .factory('securityInterceptor', ["$q","$rootScope",function ($q,$rootScope) {
            return {
                'response': function(response) {
                    var data = response.data;
                    // 判断错误码，如果是未登录
                    if(data["code"] == "-103"){
                        // 清空用户本地token存储的信息
                        $rootScope.user = {};
                        // 全局事件，方便其他view获取该事件，并给以相应的提示或处理
                        $rootScope.$emit("userIntercepted","-103",response);
                    }
                    return  response || $q.when(response);
                },
                responseError: function (response) {
                    if( response.status == '404' ){
                        // 全局事件，方便其他view获取该事件，并给以相应的提示或处理
                        $rootScope.$emit("userIntercepted","-101", response);
                    }
                    return $q.reject(response);
                }
            };
        }])
        .constant('JQ_CONFIG', {
                imgareaselect:['css/imgareaselectCss/imgareaselect-default.css',
                                'css/imgareaselectCss/imgareaselect-animated.css',
                                'vendor/jquery.imgareaselect.min.js',
                                'vendor/jquery/jquery.imgareaselect.pack.js'],
            }
        )

        .config(['$ocLazyLoadProvider','$stateProvider','$urlRouterProvider',function($ocLazyLoadProvider,$stateProvider, $urlRouterProvider){
            $ocLazyLoadProvider.config({
                loadedModules: ['qiyepeixun'],
                asyncLoader: require
            });
            // $urlRouterProvider.otherwise('/app/proIndex');
            $urlRouterProvider.otherwise('/app/myCourse');
            $stateProvider
                .state("access",{
                    url:"/access",
                    template: '<div ui-view style="height:100%"></div>',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/appCtrl.js', 'css/login.css?v='+v]);
                        }
                    }
                })
                .state("access.login",{
                    url:"/login",
                    controller: 'login',
                    templateUrl: 'tpl/login/login.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/login/login.js']);
                        }
                    }
                })
                .state("access.register",{
                    url:"/register",
                    controller: 'register',
                    templateUrl: 'tpl/login/register.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/login/register.js']);
                        }
                    }
                })
                .state("access.forgetPwd",{
                    url:"/forgetPwd",
                    controller: 'forgetPwd',
                    templateUrl: 'tpl/login/forgetPwd.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/login/forgetPwd.js']);
                        }
                    }
                })
                .state("app",{
                    url:"/app",
                    controller: 'appCtrl',
                    templateUrl: 'tpl/layout/heading.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/appCtrl.js']);
                        }
                    }
                })
                .state("app.compProfession",{
                    url:"/compProfession",
                    controller: 'compProfession',
                    templateUrl: 'tpl/company/compProfession.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/company/compProfession.js']);
                        }
                    }
                })
                .state("app.compLive",{
                    url:"/compLive",
                    controller: 'compLive',
                    templateUrl: 'tpl/company/compLive.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/company/compLive.js']);
                        }
                    }
                })
                .state("app.compCourse",{
                    url:"/compCourse/:cate_id/:specialty_id",
                    controller: 'compCourse',
                    templateUrl: 'tpl/company/compCourse.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/company/compCourse.js']);
                        }
                    }
                })
                .state("app.myCoursenew",{
                    url:"/myCoursenew/:cate_id/:specialty_id",
                    controller: 'myCoursenew',
                    templateUrl: 'tpl/myCenter/myCoursenew.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/myCenter/myCoursenew.js']);
                        }
                    }
                })
                .state("app.trainCourse",{
                    url:"/trainCourse/:curriculum_id",
                    controller: 'trainCourse',
                    templateUrl: 'tpl/company/trainCourse.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load([
                                'js/controller/company/trainCourse.js',
                                // 'bower_components/videogular-themes-default/videogular.css',
                                'css/coursePlay.css?v='+v
                                // 'js/filters/filters.js'
                            ]);
                        }
                    }
                })
                .state("app.compaliVideo",{
                    url:"/compaliVideo/:public_id/:aliyun_video_id",
                    controller: 'compaliVideo',
                    templateUrl: 'tpl/company/compaliVideo.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load([
                                'js/controller/company/compaliVideo.js',
                                'css/coursePlay.css?v='+v
                            ]);
                        }
                    }
                })
                .state("app.taskCourse",{
                    url:"/taskCourse/:curriculum_id/:curriculum_name/:index",
                    controller: 'taskCourse',
                    templateUrl: 'tpl/company/taskCourse.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/company/taskCourse.js']);
                        }
                    }
                })
                 .state("app.myCourse",{
                    url:"/myCourse",
                    controller: 'myCourse',
                    templateUrl: 'tpl/myCenter/myCourse.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/myCenter/myCourse.js']);
                        }
                    }
                })
                .state("app.myTask",{
                    url:"/myTask",
                    controller: 'myTask',
                    templateUrl: 'tpl/myCenter/myTask.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/myCenter/myTask.js']);
                        }
                    }
                })
                 .state("app.compReport",{
                    url:"/compReport",
                    controller: 'compReport',
                    templateUrl: 'tpl/company/compReport.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/company/compReport.js']);
                        }
                    }
                })
                 .state("app.compMember",{
                    url:"/compMember",
                    controller: 'compMember',
                    templateUrl: 'tpl/company/compMember.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/company/compMember.js'],['js/jquery-3.1.0.min.js']);
                        }
                    }
                })
                .state("app.nenglitest",{
                    url:"/nenglitest",
                    controller: 'nenglitest',
                    templateUrl: 'tpl/company/nenglitest.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/company/nenglitest.js']);
                        }
                    }
                })
                .state("app.compSet",{
                    url:"/compSet",
                    controller: 'compSet',
                    templateUrl: 'tpl/company/compSet.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/company/compSet.js']);
                        }
                    }
                })
                .state("app.compTask",{
                    url:"/compTask",
                    controller: 'compTask',
                    templateUrl: 'tpl/company/compTask.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/company/compTask.js']);
                        }
                    }
                })
                .state("app.compActivity",{
                    url:"/compActivity",
                    controller: 'compActivity',
                    templateUrl: 'tpl/company/compActivity.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/company/compActivity.js']);
                        }
                    }
                })
                .state("app.accountInfo",{
                    url:"/accountInfo",
                    controller: 'accountInfo',
                    templateUrl: 'tpl/accountCenter/accountInfo.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/accountCenter/accountInfo.js','js/directives/fileread.js']);
                        }
                    }
                })
                .state("app.circularize",{
                    url:"/circularize",
                    controller: 'CircuLarize',
                    templateUrl: 'tpl/circularize/circularize.html',
                    resolve: {
                        load: function ($ocLazyLoad) {
                            return $ocLazyLoad.load(['js/controller/circularize/circularize.js']);
                        }
                    }
                })
                // .state("app.proIndex",{
                //     url:"/proIndex",
                //     controller:"proIndex",
                //     templateUrl:"tpl/product/proIndex.html",
                //     resolve:{
                //         load:function($ocLazyLoad){
                //             return $ocLazyLoad.load(['js/controller/product/proIndex.js'])
                //         }
                //     }
                // })
                // .state("app.signUp",{
                //     url:"/signUp",
                //     controller:"signUp",
                //     templateUrl:"tpl/product/signUp.html",
                //     resolve:{
                //         load:function($ocLazyLoad){
                //             return $ocLazyLoad.load(['js/controller/product/signUp.js'])
                //         }
                //     }
                // })
                // .state("app.pay",{
                //     url:"/pay:paydata",
                //     controller:"pay",
                //     templateUrl:"tpl/product/pay.html",
                //     resolve:{
                //         load:function($ocLazyLoad){
                //             return $ocLazyLoad.load(['js/controller/product/pay.js'])
                //         }
                //     }
                // })
                .state("app.myMajor",{
                    url:"/myMajor",
                    controller:"myMajor",
                    templateUrl:"tpl/myCenter/myMajor.html",
                    resolve:{
                        load:function($ocLazyLoad){
                            return $ocLazyLoad.load(['js/controller/myCenter/myMajor.js'])
                        }
                    }
                })
                .state("app.abilityAssess",{
                    url:"/abilityAssess",
                    controller:"abilityAssess",
                    templateUrl:"tpl/company_tool/abilityAssess.html",
                    resolve:{
                        load:function($ocLazyLoad){
                            return $ocLazyLoad.load(['js/controller/company_tool/abilityAssess.js'])
                        }
                    }
                })
                .state("app.ablityAssessRecord",{
                    url:"/ablityAssessRecord",
                    controller:"ablityAssessRecord",
                    templateUrl:"tpl/myCenter/ablityAssessRecord.html",
                    resolve:{
                        load:function($ocLazyLoad){
                            return $ocLazyLoad.load(['js/controller/myCenter/ablityAssessRecord.js'])
                        }
                    }
                })
                .state("app.reportDetail",{
                    url:"/reportDetail/:ability_check_id/:type",
                    controller:"reportDetail",
                    templateUrl:"tpl/company_tool/reportDetail.html",
                    resolve:{
                        load:function($ocLazyLoad){
                            return $ocLazyLoad.load(['js/controller/company_tool/reportDetail.js'])
                        }
                    }
                })
                .state("app.informationEntry",{
                    url:"/informationEntry/:token",
                    controller:"informationEntry",
                    templateUrl:"tpl/capacityAssessment/informationEntry.html",
                    resolve:{
                        load:function($ocLazyLoad){
                            return $ocLazyLoad.load(['js/controller/capacityAssessment/informationEntry.js'])
                        }
                    }
                })
                .state("app.reviewtheTopic",{
                    url:"/reviewtheTopic/:obj/:token",
                    controller:"reviewtheTopic",
                    templateUrl:"tpl/capacityAssessment/reviewtheTopic.html",
                    resolve:{
                        load:function($ocLazyLoad){
                            return $ocLazyLoad.load(['js/controller/capacityAssessment/reviewtheTopic.js'])
                        }
                    }
                })
        }]);
    return app;
});
